<?php
/**
 * --------------------------------------------------------------
 * 附件控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\Attachment as AttachmentModel;
use think\Image;
use think\File;

class Attachment extends AdminBase
{
    /**
     * 附件列表
     */
    public function index()
    {
        $map  = $this->getSimpleSearchMap();
        $list = AttachmentModel::where($map['where'])
                ->order('id desc')
                ->paginate(config('app.site_setting.list_rows'), false, [
                    'query' => $map['conditions']
                ]);

        $this->assign([
            'list'         => $list,
            'keyword'      => $map['conditions']['keyword'],
            'search_field' => $map['conditions']['search_field'],
            'title'        => '附件列表'
        ]);
        return $this->fetch();
    }

    /**
     * 上传附件
     * @param string $dir 保存的目录:images,files,videos,voices
     * @param string $from 来源，wangeditor：wangEditor编辑器, ueditor:ueditor编辑器, editormd:editormd编辑器等
     * @author 蔡伟明 <314013107@qq.com>
     * @return \think\response\Json|void
     */
    public function upload($dir = '', $from = '')
    {
        if ($dir == '')         return $this->error('没有指定上传目录');
        if ($from == 'ueditor') return $this->ueditor();
        if ($from == 'jcrop')   return $this->jcrop();
        return $this->saveFile($dir, $from);
    }

    /**
     * 保存附件
     * @param string $dir 附件存放的目录
     * @param string $from 来源
     * @author 蔡伟明 <314013107@qq.com>
     * @return string|\think\response\Json
     */
    private function saveFile($dir = '', $from = '')
    {
        // 附件大小限制
        $size_limit = $dir == 'images' ? config('app.site_setting.upload_image_size') : config('app.site_setting.upload_file_size');
        $size_limit = $size_limit * 1024;
        // 附件类型限制
        $ext_limit = $dir == 'images' ? config('app.site_setting.upload_image_ext') : config('app.site_setting.upload_file_ext');
        $ext_limit = $ext_limit != '' ? parse_attr($ext_limit) : '';

        // 获取附件数据
        switch ($from) {
            case 'editormd':
                $file_input_name = 'editormd-image-file';
                break;
            case 'ckeditor':
                $file_input_name = 'upload';
                break;
            default:
                $file_input_name = 'file';
        }
        $file = $this->request->file($file_input_name);

        // 判断附件是否已经存在
        if ($file_exists = AttachmentModel::get(['md5' => $file->hash('md5')])) {
            $file_path = $file_exists['path'];
            switch ($from) {
                case 'wangeditor':
                    return json([
                        'errno' => 0,
                        'url'   => $file_path
                    ]);
                    break;
                case 'ueditor':
                    return json([
                        'state' => 'SUCCESS',
                        'url'   => $file_path,
                        'title' => $file_exists['name'],
                    ]);
                    break;
                case 'editormd':
                    return json([
                        'success' => 1,
                        'message' => '上传成功',
                        'url'     => $file_path,
                    ]);
                    break;
                case 'ckeditor':
                    return json([
                        'uploaded' => 1,
                        'fileName' => $file_exists['name'],
                        'url'      => $file_path
                    ]);
                    break;
                default:
                    return json([
                        'status' => 1,
                        'info'   => '上传成功',
                        'class'  => 'bg-success',
                        'id'     => $file_exists['id'],
                        'path'   => $file_path
                    ]);
            }
        }

        // 判断附件大小是否超过限制
        if ($size_limit > 0 && ($file->getInfo('size') > $size_limit)) {
            switch ($from) {
                case 'wangeditor':
                    return json(['errno' => '附件过大']);
                    break;
                case 'ueditor':
                    return json(['state' => '附件过大']);
                    break;
                case 'editormd':
                    return json(["success" => 0, "message" => '附件过大']);
                    break;
                case 'ckeditor':
                    return json([
                        'uploaded' => 0,
                        'error'    => ['message' => '附件过大']
                    ]);
                    break;
                default:
                    return json([
                        'status' => 0,
                        'class'  => 'bg-danger',
                        'info'   => '附件过大'
                    ]);
            }
        }

        // 判断附件格式是否符合
        $file_name = $file->getInfo('name');
        $file_ext  = strtolower(substr($file_name, strrpos($file_name, '.') + 1));

        $error_msg = '';
        if ($ext_limit == '') {
            $error_msg = '获取文件信息失败';
        }
        if ($file->getMime() == 'text/x-php' || $file->getMime() == 'text/html') {
            $error_msg = '禁止上传非法文件';
        }
        if (!in_array($file_ext, $ext_limit)) {
            $error_msg = '附件类型不正确';
        }
        if ($error_msg != '') {
            switch ($from) {
                case 'wangeditor':
                    return json(['errno' => $error_msg]);
                    break;
                case 'ueditor':
                    return json(['state' => $error_msg]);
                    break;
                case 'editormd':
                    return json(['success' => 0, 'message' => $error_msg]);
                    break;
                case 'ckeditor':
                    return json([
                        'uploaded' => 0,
                        'error'    => ['message' => $error_msg]
                    ]);
                    break;
                default:
                    return json([
                        'status' => 0,
                        'class'  => 'bg-danger',
                        'info'   => $error_msg
                    ]);
            }
        }

        // 移动到/uploads/目录下，路径设置在config/app.php内
        $info = $file->move(config('upload_path') . DIRECTORY_SEPARATOR . $dir);

        if ($info) {
            // 水印功能
            if ($dir == 'images' && config('app.site_setting.upload_thumb_water') == 1 && config('app.site_setting.upload_thumb_water_pic') > 0) {
                $this->create_water($info->getRealPath());
            }
            
            // 缩略图路径
            $thumb_path_name = '';
            if ($dir == 'images' && config('app.site_setting.upload_image_thumb') != '') {
                $thumb_path_name = $this->create_thumb($info, $info->getPathInfo()->getfileName(), $info->getFilename());
            }

            // 获取附件信息
            $file_info = [
                'aid'   => session('admin_auth.aid'),
                'name'  => $file->getInfo('name'),
                'mime'  => $file->getInfo('type'),
                'path'  => '/uploads/' . $dir . '/' . str_replace('\\', '/', $info->getSaveName()),
                'ext'   => $info->getExtension(),
                'size'  => $info->getSize(),
                'md5'   => $info->hash('md5'),
                'sha1'  => $info->hash('sha1'),
                'thumb' => $thumb_path_name,
            ];

            if ($file_add = AttachmentModel::create($file_info)) {
                $file_path = $file_info['path'];
                switch ($from) {
                    case 'wangeditor':
                        return json([
                            'errno' => 0,
                            'url'   => $file_path
                        ]);
                        break;
                    case 'ueditor':
                        return json([
                            'state' => 'SUCCESS',
                            'url'   => $file_path,
                            'title' => $file_info['name']
                        ]);
                        break;
                    case 'editormd':
                        return json([
                            'success' => 1,
                            'message' => '上传成功',
                            'url'     => $file_path
                        ]);
                        break;
                    case 'ckeditor':
                        return json([
                            'uploaded' => 1,
                            'fileName' => $file_info['name'],
                            'url'      => $file_path
                        ]);
                        break;
                    default:
                        return json([
                            'status' => 1,
                            'info'   => '上传成功',
                            'class'  => 'bg-success',
                            'id'     => $file_add['id'],
                            'path'   => $file_path
                        ]);
                }
            } else {
                switch ($from) {
                    case 'wangeditor':
                        return json(['errno' => '上传失败']);
                        break;
                    case 'ueditor':
                        return json(['state' => '上传失败']);
                        break;
                    case 'editormd':
                        return json(["success" => 0, "message" => '上传失败']);
                        break;
                    case 'ckeditor':
                        return json([
                            'uploaded' => 0,
                            'error'    => ['message' => '上传失败']
                        ]);
                        break;
                    default:
                        return json(['status' => 0, 'class' => 'bg-danger', 'info' => '上传失败']);
                }
            }
        } else {
            switch ($from) {
                case 'wangeditor':
                    return json(['errno' => $file->getError()]);
                    break;
                case 'ueditor':
                    return json(['state' => '上传失败']);
                    break;
                case 'editormd':
                    return json(["success" => 0, "message" => '上传失败']);
                    break;
                case 'ckeditor':
                    return json([
                        'uploaded' => 0,
                        'error'    => ['message' => '上传失败']
                    ]);
                    break;
                default:
                    return json(['status' => 0, 'class' => 'bg-danger', 'info' => $file->getError()]);
            }
        }
    }

    /**
     * 处理ueditor上传
     * @author 蔡伟明 <314013107@qq.com>
     * @return string|\think\response\Json
     */
    private function ueditor()
    {
        $action      = $this->request->get('action');
        $config_file = env('root_path') . 'public/static/js/ueditor/php/config.json';
        $config      = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents($config_file)), true);
        switch ($action) {
            /* 获取配置信息 */
            case 'config':
                $result = $config;
                break;

            /* 上传图片 */
            case 'uploadimage':
            /* 上传涂鸦 */
            case 'uploadscrawl':
                return $this->saveFile('images', 'ueditor');
                break;

            /* 上传视频 */
            case 'uploadvideo':
                return $this->saveFile('videos', 'ueditor');
                break;

            /* 上传附件 */
            case 'uploadfile':
                return $this->saveFile('files', 'ueditor');
                break;

            /* 列出图片 */
            case 'listimage':
                return $this->showFile('listimage', $config);
                break;

            /* 列出附件 */
            case 'listfile':
                return $this->showFile('listfile', $config);
                break;

            default:
                $result = ['state' => '请求地址出错'];
                break;
        }

        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                return json(['state' => 'callback参数不合法']);
            }
        } else {
            return json($result);
        }
    }

    /**
     * 显示附件列表（ueditor）
     * @param string $type 类型
     * @param $config
     * @author 蔡伟明 <314013107@qq.com>
     * @return \think\response\Json
     */
    private function showFile($type = '', $config)
    {
        /* 判断类型 */
        switch ($type) {
            /* 列出附件 */
            case 'listfile':
                $allowFiles = $config['fileManagerAllowFiles'];
                $listSize   = $config['fileManagerListSize'];
                $path       = realpath(config('upload_path') .'/files/');
                break;
            /* 列出图片 */
            case 'listimage':
            default:
                $allowFiles = $config['imageManagerAllowFiles'];
                $listSize   = $config['imageManagerListSize'];
                $path       = realpath(config('upload_path') .'/images/');
        }
        $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);

        /* 获取参数 */
        $size  = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
        $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
        $end   = $start + $size;

        /* 获取附件列表 */
        $files = $this->getfiles($path, $allowFiles);
        if (!count($files)) {
            return json([
                "state" => "no match file",
                "list"  => [],
                "start" => $start,
                "total" => count($files)
            ]);
        }

        /* 获取指定范围的列表 */
        $len = count($files);
        for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
            $list[] = $files[$i];
        }
        //倒序
        //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
        //    $list[] = $files[$i];
        //}

        /* 返回数据 */
        $result = array(
            "state" => "SUCCESS",
            "list"  => $list,
            "start" => $start,
            "total" => count($files)
        );

        return json($result);
    }

    /**
     * 处理Jcrop图片裁剪
     * @author 蔡伟明 <314013107@qq.com>
     */
    private function jcrop()
    {
        $file_path = $this->request->post('path', '');
        $cut_info  = $this->request->post('cut', '');
        
        if ($file_path == '') {
            $file = $this->request->file('file');
            if (!is_dir(config('upload_temp_path'))) {
                mkdir(config('upload_temp_path'), 0766, true);
            }
            $info = $file->move(config('upload_temp_path'), $file->hash('md5'));
            if ($info) {
                return json(['code' => 1, 'src' => '/uploads/temp/'. $info->getFilename()]);
            } else {
                $this->error('上传失败');
            }
        }

        $file_path = config('upload_temp_path') . str_replace('/uploads/temp/', '', $file_path);

        if (is_file($file_path)) {
            // 获取裁剪信息
            $cut_info  = explode(',', $cut_info);

            // 读取图片
            $image = Image::open($file_path);

            $dir_name = date('Ymd');
            $file_dir = config('upload_path') . '/' . 'images/' . $dir_name . '/';
            if (!is_dir($file_dir)) {
                mkdir($file_dir, 0766, true);
            }
            $file_name     = md5(microtime(true)) . '.' . $image->type();
            $new_file_path = $file_dir . $file_name;

            // 裁剪图片
            $image->crop($cut_info[0], $cut_info[1], $cut_info[2], $cut_info[3], $cut_info[4], $cut_info[5])->save($new_file_path);

            // 水印功能
            if (config('app.site_setting.upload_thumb_water') == 1 && config('app.site_setting.upload_thumb_water_pic') > 0) {
                $this->create_water($new_file_path);
            }

            // 是否创建缩略图
            $thumb_path_name = '';
            if (config('app.site_setting.upload_image_thumb') != '') {
                $thumb_path_name = $this->create_thumb($new_file_path, $dir_name, $file_name);
            }

            // 保存图片
            $file = new File($new_file_path);
            $file_info = [
                'uid'    => session('admin_auth.uid'),
                'name'   => $file_name,
                'mime'   => $image->mime(),
                'path'   => '/uploads/images/' . $dir_name . '/' . $file_name,
                'ext'    => $image->type(),
                'size'   => $file->getSize(),
                'md5'    => $file->hash('md5'),
                'sha1'   => $file->hash('sha1'),
                'thumb'  => $thumb_path_name
            ];

            if ($file_add = AttachmentModel::create($file_info)) {
                // 删除临时图片
                unlink($file_path);
                // 返回成功信息
                return json([
                    'code'  => 1,
                    'id'    => $file_add['id'],
                    'src'   => $file_info['path'],
                    'thumb' => $thumb_path_name == '' ? '' : $thumb_path_name,
                ]);
            } else {
                $this->error('上传失败');
            }
        }
        $this->error('文件不存在');
    }

    /**
     * 创建缩略图
     * @param string $file 目标文件，可以是文件对象或文件路径
     * @param string $dir 保存目录，即目标文件所在的目录名
     * @param string $save_name 缩略图名
     * @author 蔡伟明 <314013107@qq.com>
     * @return string 缩略图路径
     */
    private function create_thumb($file = '', $dir = '', $save_name = '')
    {
        // 获取要生成的缩略图最大宽度和高度
        list($thumb_max_width, $thumb_max_height) = explode(',', config('app.site_setting.upload_image_thumb'));
        // 读取图片
        $image = Image::open($file);
        // 生成缩略图
        $image->thumb($thumb_max_width, $thumb_max_height, config('app.site_setting.upload_image_thumb_type'));
        // 保存缩略图
        $thumb_path = config('upload_path') . DIRECTORY_SEPARATOR . 'images/' . $dir . '/thumb/';
        if (!is_dir($thumb_path)) {
            mkdir($thumb_path, 0766, true);
        }
        $thumb_path_name = $thumb_path. $save_name;
        $image->save($thumb_path_name);
        $thumb_path_name = 'uploads/images/' . $dir . '/thumb/' . $save_name;

        return $thumb_path_name;
    }

    /**
     * 添加水印
     * @param string $file 要添加水印的文件路径
     * @author 蔡伟明 <314013107@qq.com>
     */
    private function create_water($file = '')
    {
        $path = model('admin/attachment')->getFilePath(config('app.site_setting.upload_thumb_water_pic'));
        $thumb_water_pic = realpath(ROOT_PATH . 'public/' . $path);

        // 读取图片
        $image = Image::open($file);
        // 添加水印
        $image->water($thumb_water_pic, config('app.site_setting.upload_thumb_water_position'), config('app.site_setting.upload_thumb_water_alpha'));
        // 保存水印图片，覆盖原图
        $image->save($file);
    }

    /**
     * 遍历获取目录下的指定类型的附件
     * @param string $path 路径
     * @param string $allowFiles 允许查看的类型
     * @param array $files 文件列表
     * @author 蔡伟明 <314013107@qq.com>
     * @return array|null
     */
    public function getfiles($path = '', $allowFiles = '', &$files = array())
    {
        if (!is_dir($path)) return null;
        if(substr($path, strlen($path) - 1) != '/') $path .= '/';
        $handle = opendir($path);
        while (false !== ($file = readdir($handle))) {
            if ($file != '.' && $file != '..') {
                $path2 = $path . $file;
                if (is_dir($path2)) {
                    $this->getfiles($path2, $allowFiles, $files);
                } else {
                    if (preg_match("/\.(".$allowFiles.")$/i", $file)) {
                        $files[] = array(
                            'url'=> str_replace("\\", "/", substr($path2, strlen($_SERVER['DOCUMENT_ROOT']))),
                            'mtime'=> filemtime($path2)
                        );
                    }
                }
            }
        }
        return $files;
    }
	
	/**
	 * 禁用/启用
	 * @param array $ids id数组
     * @param integer $status 状态值
     */
    public function setStatus($ids = [], $status)
    {
        empty($ids) && $this->error('请选择要操作的数据');
        !is_numeric((int) $status) && $this->error('参数错误');
        
        $map  = ['id' => $ids];
        $info = AttachmentModel::where($map)->setField('status', $status);
        $tips = $status == 1 ? '启用' : '禁用';
        return $info !== FALSE ? $this->success($tips.'成功') : $this->error($tips.'失败');
    }

    /**
     * 删除附件
     * @param string $ids 附件id
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     */
    public function delete($ids = '')
    {
        $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        if (empty($ids)) return $this->error('缺少参数ss');

        $files_path = AttachmentModel::where('id', 'in', $ids)->column('path,thumb', 'id');

        foreach ($files_path as $value) {
            $real_path       = realpath(config('upload_path').'/../'.$value['path']);
            $real_path_thumb = realpath(config('upload_path').'/../'.$value['thumb']);

            if (is_file($real_path) && !unlink($real_path)) {
                $this->error('删除失败');
            }
            if (is_file($real_path_thumb) && !unlink($real_path_thumb)) {
                $this->error('删除缩略图失败');
            }
        }
        if (AttachmentModel::where('id', 'in', $ids)->delete()) {
            // 记录行为
            $ids = is_array($ids) ? implode(',', $ids) : $ids;
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }
}